package org.edu.modules.sxjy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.transaction.Transactional;
import org.edu.common.api.vo.Result;
import org.edu.modules.sxjy.entity.VeBaseAppUser;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VePost;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.mapper.VeCompanyMapper;
import org.edu.modules.sxjy.mapper.VeCompanyUserMapper;
import org.edu.modules.sxjy.service.IVeCompanyService;
import org.edu.modules.sxjy.service.IVeCompanyUserService;
import org.edu.modules.sxjy.vo.VeCompanyUserAddVo;
import org.edu.modules.sxjy.vo.VeCompanyUserUpdateVo;
import org.edu.modules.sxjy.vo.VeCompanyUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeCompanyUserServiceImpl
        extends ServiceImpl<VeCompanyUserMapper, VeCompanyUser>
        implements IVeCompanyUserService
{
    @Autowired
    private VeCompanyUserMapper veCompanyUserMapper;
    @Autowired
    private IVeCompanyService veCompanyService;
    @Autowired
    private IVeCompanyUserService veCompanyUserService;
    @Autowired
    private VeCommonClient veCommonClient;
    @Autowired
    private VeCompanyMapper veCompanyMapper;

    public List<VeCompanyUserVo> selectAll(VeCompanyUserVo veCompanyUserVo, Integer pageNo, Integer pageSize, VeCommon veCommon)
    {
        List<VeCompanyUserVo> veCompanyUserVoList = new ArrayList();
        VeCompanyUser veCompanyUser;
        if (veCommon.getUserType().contains("3"))
        {
            veCompanyUser = this.veCompanyMapper.getCpUserByUserId(veCommon.getUserId());
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
            {
                veCommon.setCpId(veCompanyUser.getCpId());
                veCompanyUserVoList = this.veCompanyUserMapper.selectAll(veCompanyUserVo, pageNo, pageSize, veCommon);
            }
            else if ((veCompanyUser == null) && ("".equals(veCompanyUser)))
            {
                veCompanyUserVoList = new ArrayList();
            }
        }
        else
        {
            veCompanyUserVoList = this.veCompanyUserMapper.selectAll(veCompanyUserVo, pageNo, pageSize, veCommon);
        }
        if ((veCompanyUserVoList != null) && (veCompanyUserVoList.size() > 0)) {
            for (VeCompanyUserVo companyUserVo : veCompanyUserVoList) {
                if (companyUserVo.getType().equals("cpadmin")) {
                    companyUserVo.setType("企业管理员");
                } else if (companyUserVo.getType().equals("cpteacher")) {
                    companyUserVo.setType("企业教师");
                }
            }
        }
        return veCompanyUserVoList;
    }

    public List<VeCompanyUserVo> exportXls(VeCommon veCommon)
    {
        List<VeCompanyUserVo> veCompanyUserVos = new ArrayList();
        VeCompanyUser veCompanyUser;
        if (veCommon.getUserType().contains("3"))
        {
            veCompanyUser = this.veCompanyMapper.getCpUserByUserId(veCommon.getUserId());
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
            {
                veCommon.setCpId(veCompanyUser.getCpId());
                veCompanyUserVos = this.veCompanyUserMapper.exportXls(veCommon);
            }
            else if ((veCompanyUser == null) && ("".equals(veCompanyUser)))
            {
                veCompanyUserVos = new ArrayList();
            }
        }
        else
        {
            veCompanyUserVos = this.veCompanyUserMapper.exportXls(veCommon);
        }
        for (VeCompanyUserVo veCompanyUserVo : veCompanyUserVos) {
            if (veCompanyUserVo.getType().equals("cpadmin")) {
                veCompanyUserVo.setType("企业管理员");
            } else if (veCompanyUserVo.getType().equals("cpteacher")) {
                veCompanyUserVo.setType("企业教师");
            }
        }
        return veCompanyUserVos;
    }

    public int getNameId(String name)
    {
        return this.veCompanyUserMapper.getNameId(name);
    }

    public List<VeCompanyUser> getTeacharName()
    {
        return this.veCompanyUserMapper.getTeacharName();
    }

    @Transactional
    public Result<?> saves(VeCompanyUserAddVo veCompanyUserAddVo)
    {
        VeCompanyUser veCompanyUser = new VeCompanyUser();



        veCompanyUser.setCpId(veCompanyUserAddVo.getCpId());


        veCompanyUser.setAccount(veCompanyUserAddVo.getAccount());
        if (veCompanyUserAddVo.getRealName().length() > 40) {
            return Result.error("姓名长度过大");
        }
        veCompanyUser.setRealName(veCompanyUserAddVo.getRealName());
        if (veCompanyUserAddVo.getSex().equals("0")) {
            veCompanyUser.setSex("女");
        } else if (veCompanyUserAddVo.getSex().equals("1")) {
            veCompanyUser.setSex("男");
        }
        veCompanyUser.setTel(veCompanyUserAddVo.getTel());

        veCompanyUser.setEmail(veCompanyUserAddVo.getEmail());

        veCompanyUser.setAddress(veCompanyUserAddVo.getAddress());

        VeBaseAppUser veBaseAppUser = new VeBaseAppUser();
        veBaseAppUser.setUserId(veCompanyUserAddVo.getAccount());
        if (veCompanyUserAddVo.getType().equals("cpadmin"))
        {
            veBaseAppUser.setUserType("3");

            veBaseAppUser.setUserRoleId(Integer.valueOf(37));
        }
        else if (veCompanyUserAddVo.getType().equals("cpteacher"))
        {
            veBaseAppUser.setUserType("4");

            veBaseAppUser.setUserRoleId(Integer.valueOf(38));
        }
        veBaseAppUser.setUserPassword(veCompanyUserAddVo.getPassword());
        if ((veCompanyUserAddVo.getTel() != null) && (!"".equals(veCompanyUserAddVo.getTel()))) {
            veBaseAppUser.setUserTel(veCompanyUserAddVo.getTel());
        }
        veBaseAppUser.setInterfaceUserId("09d5e1e7f9b049008eee645c783a1d67");
        if (veCompanyUserAddVo.getRealName().length() > 40) {
            return Result.error("姓名长度过大");
        }
        veBaseAppUser.setRealName(veCompanyUserAddVo.getRealName());

        Result<?> veBaseAppUserAndSysUser = this.veCommonClient.addVeBaseAppUser(veBaseAppUser);
        boolean equals = veBaseAppUserAndSysUser.getCode().equals(Integer.valueOf(200));
        if (!equals) {
            return veBaseAppUserAndSysUser;
        }
        veCompanyUser.setUserId(veCompanyUserAddVo.getAccount());


        veCompanyUser.setType(veCompanyUserAddVo.getType());

        veCompanyUser.setTerminalId(1);

        this.veCompanyUserService.save(veCompanyUser);

        return Result.ok("添加成功");
    }

    public int getEnterNameId(String name)
    {
        return this.veCompanyUserMapper.getEnterNameId(name);
    }

    public boolean setExcel(VeCompanyUserVo veCompanyUserVo)
    {
        VeCompanyUser veCompanyUser = new VeCompanyUser();


        int firmNameId = this.veCompanyService.getFirmNameId(veCompanyUserVo.getName());
        if (firmNameId == 0) {
            return false;
        }
        veCompanyUser.setCpId(firmNameId);



        veCompanyUser.setAccount(veCompanyUserVo.getAccount());

        veCompanyUser.setRealName(veCompanyUserVo.getRealName());



        veCompanyUser.setTel(veCompanyUserVo.getTel());

        veCompanyUser.setEmail(veCompanyUserVo.getEmail());

        veCompanyUser.setAddress(veCompanyUserVo.getAddress());

        VeBaseAppUser veBaseAppUser = new VeBaseAppUser();
        veBaseAppUser.setUserId(veCompanyUserVo.getAccount());
        if (veCompanyUserVo.getType().equals("企业管理员"))
        {
            veBaseAppUser.setUserType("3");

            veBaseAppUser.setUserRoleId(Integer.valueOf(37));
        }
        else if (veCompanyUserVo.getType().equals("企业教师"))
        {
            veBaseAppUser.setUserType("4");

            veBaseAppUser.setUserRoleId(Integer.valueOf(38));
        }
        veBaseAppUser.setUserPassword(veCompanyUserVo.getPassword());
        if ((veCompanyUserVo.getTel() != null) && (!"".equals(veCompanyUserVo.getTel()))) {
            veBaseAppUser.setUserTel(veCompanyUserVo.getTel());
        }
        veBaseAppUser.setInterfaceUserId("09d5e1e7f9b049008eee645c783a1d67");
        veBaseAppUser.setRealName(veCompanyUserVo.getRealName());
        Integer code = this.veCommonClient.addVeBaseAppUser(veBaseAppUser).getCode();
        if (!code.equals(Integer.valueOf(200))) {
            return false;
        }
        veCompanyUser.setUserId(veCompanyUserVo.getAccount());
        if ((!veCompanyUserVo.getType().equals("企业管理员")) && (!veCompanyUserVo.getType().equals("企业教师")) && (!veCompanyUserVo.getType().equals("企业员工"))) {
            return false;
        }
        if (veCompanyUserVo.getType().equals("企业管理员")) {
            veCompanyUser.setType("cpadmin");
        } else if (veCompanyUserVo.getType().equals("企业教师")) {
            veCompanyUser.setType("cpteacher");
        }
        veCompanyUser.setTerminalId(1);

        this.veCompanyUserService.save(veCompanyUser);


        return true;
    }

    public VeCompanyUserUpdateVo editEcho(int id)
    {
        VeCompanyUser veCompanyUser = (VeCompanyUser)this.veCompanyUserService.getById(Integer.valueOf(id));

        VeCompanyUserUpdateVo veCompanyUserUpdateVo = new VeCompanyUserUpdateVo();
        if ((veCompanyUser == null) || ("".equals(veCompanyUser))) {
            return veCompanyUserUpdateVo;
        }
        BeanUtils.copyProperties(veCompanyUser, veCompanyUserUpdateVo);


        veCompanyUserUpdateVo.setCpId(veCompanyUser.getCpId());
        if (veCompanyUser.getType().equals("cpadmin")) {
            veCompanyUserUpdateVo.setType("企业管理员");
        } else if (veCompanyUser.getType().equals("cpteacher")) {
            veCompanyUserUpdateVo.setType("企业教师");
        }
        return veCompanyUserUpdateVo;
    }

    public Result<?> edit(VeCompanyUserUpdateVo veCompanyUserUpdateVo)
    {
        VeCompanyUser veCompanyUser = (VeCompanyUser)this.veCompanyUserService.getById(Integer.valueOf(veCompanyUserUpdateVo.getId()));
        if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
        {
            List<VePost> vePostList = this.veCompanyUserMapper.getIsPostInfoByCpUserId(veCompanyUser.getId(), veCompanyUser.getCpId());
            if ((vePostList != null) && (vePostList.size() > 0)) {
                return Result.error("将该企业教师下的岗位进行转移后才可更改用户类型");
            }
        }
        if ((veCompanyUserUpdateVo != null) && (!"".equals(veCompanyUserUpdateVo)))
        {
            veCompanyUser.setCpId(veCompanyUserUpdateVo.getCpId());

            veCompanyUser.setAccount(veCompanyUserUpdateVo.getAccount());
            if (veCompanyUserUpdateVo.getRealName().length() < 50) {
                veCompanyUser.setRealName(veCompanyUserUpdateVo.getRealName());
            } else {
                return Result.error("姓名输入过多");
            }
            veCompanyUser.setSex(veCompanyUserUpdateVo.getSex());

            veCompanyUser.setTel(veCompanyUserUpdateVo.getTel());

            veCompanyUser.setEmail(veCompanyUserUpdateVo.getEmail());
            if (veCompanyUserUpdateVo.getType().equals("企业管理员")) {
                veCompanyUserUpdateVo.setType("cpadmin");
            } else if (veCompanyUserUpdateVo.getType().equals("企业教师")) {
                veCompanyUserUpdateVo.setType("cpteacher");
            }
            if (veCompanyUser.getType() != veCompanyUserUpdateVo.getType())
            {
                VeBaseAppUser veBaseAppUser = new VeBaseAppUser();
                veBaseAppUser.setUserId(veCompanyUserUpdateVo.getAccount());
                veBaseAppUser.setInterfaceUserId("09d5e1e7f9b049008eee645c783a1d67");
                veBaseAppUser.setUserType(veCompanyUserUpdateVo.getUserType());
                if (veCompanyUserUpdateVo.getType().equals("cpadmin"))
                {
                    veBaseAppUser.setUserType("3");

                    veBaseAppUser.setUserRoleId(Integer.valueOf(37));
                }
                else if (veCompanyUserUpdateVo.getType().equals("cpteacher"))
                {
                    veBaseAppUser.setUserType("4");

                    veBaseAppUser.setUserRoleId(Integer.valueOf(38));
                }
                this.veCommonClient.editVeBaseAppUser(veBaseAppUser);
            }
            veCompanyUser.setType(veCompanyUserUpdateVo.getType());

            veCompanyUser.setAddress(veCompanyUserUpdateVo.getAddress());

            veCompanyUser.setTerminalId(1);
        }
        this.veCompanyUserService.updateById(veCompanyUser);
        return Result.OK("编辑成功");
    }

    public List<VeCompanyUser> getTeacharNameByCpId(int cpId)
    {
        return this.veCompanyUserMapper.getTeacharNameByCpId(cpId);
    }

    public VeCompanyUser getCpInfoByUserId(String userId)
    {
        return this.veCompanyUserMapper.getCpInfoByUserId(userId);
    }

    public int getSumPage(VeCompanyUserVo veCompanyUserVo, VeCommon veCommon)
    {
        return this.veCompanyUserMapper.getSumPage(veCompanyUserVo, veCommon);
    }

    public Result<?> delete(String id)
    {
        VeCompanyUser veCompanyUser = (VeCompanyUser)this.veCompanyUserService.getById(id);
        if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
        {
            List<VePost> vePostList = this.veCompanyUserMapper.getIsPostInfoByCpUserId(Integer.parseInt(id), veCompanyUser.getCpId());
            if ((vePostList != null) && (vePostList.size() > 0)) {
                return Result.error("转移该教师下的岗位后才可进行删除");
            }
        }
        this.veCommonClient.deleteAppUser("09d5e1e7f9b049008eee645c783a1d67", id);
        this.veCompanyUserService.removeById(id);
        return Result.OK("删除成功");
    }

    public Result<?> deleteBatch(String ids)
    {
        List<String> stringList = Arrays.asList(ids.split(","));
        Iterator localIterator = stringList.iterator();
        if (localIterator.hasNext())
        {
            String id = (String)localIterator.next();
            VeCompanyUser veCompanyUser = (VeCompanyUser)this.veCompanyUserService.getById(id);
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
            {
                List<VePost> vePostList = this.veCompanyUserMapper.getIsPostInfoByCpUserId(Integer.parseInt(id), veCompanyUser.getCpId());
                if ((vePostList != null) && (vePostList.size() > 0)) {
                    return Result.error("转移该教师下的岗位后才可进行删除");
                }
            }
            this.veCommonClient.deleteAppUser("09d5e1e7f9b049008eee645c783a1d67", id);
            this.veCompanyUserService.removeById(id);
            return Result.OK("删除成功");
        }
        return Result.OK();
    }
}
